﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTCaller
{
    public partial class PrepareCallTaxiPanel : UserControl
    {

        public PrepareCallTaxiPanel()
        {
            InitializeComponent();
        }

        DateTime m_dtMsgShow = System.DateTime.Now;
        DateTime m_dtRing = System.DateTime.Now;

        bool m_bWaitCallTaxi = false;
        DateTime m_dtWaitCallTaxiResult = System.DateTime.Now;
        public string m_strLastOrderID = "";
        public void initial()
        {
            this.panel_Msg.Visible = false;

            button_Taxi1.Enabled = true;
            button_Taxi2.Enabled = true;
            button_Taxi3.Enabled = true;

            this.label_SelectNeedTaxi.Text = "請選擇所需車輛數：?";
            this.button_StartCallTaxi.Visible = false;
            this.button_Cancel.Enabled = true;

            this.timer_Tick.Enabled = true;

            m_strLastOrderID = "";
            setReDispatchButton(0);


        }
        private void setReDispatchButton(int arg_n)
        {
            switch (arg_n)
            {
                case 1:
                    this.button_ReDispatch.Tag = "1";
                    this.button_ReDispatch.Visible = true;
                    this.button_Confirm.Left = 218;
                    break;
                case 2:
                    this.button_ReDispatch.Visible = false;
                    this.button_ReDispatch.Tag = "2";
                    this.button_Confirm.Left = 122;
                    break;
                default:
                    this.button_ReDispatch.Visible = false;
                    this.button_ReDispatch.Tag = "0";
                    this.button_Confirm.Left = 122;
                    break;

            }
        }

        private void button_Taxi1_Click(object sender, EventArgs e)
        {
            button_Taxi1.Enabled = true;
            button_Taxi2.Enabled = true;
            button_Taxi3.Enabled = true;
            Button b = (Button)sender;
            this.label_SelectNeedTaxi.Text = "請選擇所需車輛數：" + b.Text;
            this.button_StartCallTaxi.Tag = b.Tag;
            this.button_StartCallTaxi.Visible = true;
            b.Enabled = false;
        }

        public void NoTaxiMsg(string arg_strText, int arg_nSecs, bool arg_bShowButton, string arg_strOrderID)
        {
            m_strLastOrderID = arg_strOrderID;
            setAlert();
            ShowNoTaxiMsg(arg_strText, arg_nSecs, arg_bShowButton);

            if ("0" == (string)this.button_ReDispatch.Tag)
            {
                setReDispatchButton(1);

            }
            else
            {
                setReDispatchButton(0);
            }
        }

        public void ShowNoTaxiMsg(string arg_strText, int arg_nSecs, bool arg_bShowButton)
        {
            this.button_Confirm.Visible = arg_bShowButton;
            this.label_Msg.Text = arg_strText;
            this.label_Msg.Tag = arg_strText;
            m_dtMsgShow = System.DateTime.Now.AddSeconds(arg_nSecs);




            this.panel_Msg.Visible = true;

        }

        private void button_StartCallTaxi_Click(object sender, EventArgs e)
        {
            setReDispatchButton(0);

            m_bWaitCallTaxi = true;
            m_dtWaitCallTaxiResult = System.DateTime.Now.AddSeconds(60);

            SetToNoPrepare();

            Form_Main.Main.m_strLastCallTaxiCount =  System.Convert.ToString(button_StartCallTaxi.Tag);
            string strResult = CmdTalker.getInstance().SendServerAndWaitResponse("CI0" + button_StartCallTaxi.Tag + "0" + StoreData.getInstance().TEL + "0", 5 , "MOK", true);

            ShowNoTaxiMsg("正在為您派車中, 請稍候...", 65, false);

            if (strResult == "MOK")
            {
                //wait result

            }
            else
            {
                //fail , re send
            }

        }

        private void ResetToPrepare()
        {
            this.panel_Msg.Visible = false;
            this.button_Cancel.Enabled = true;
            this.button_StartCallTaxi.Enabled = true;
            this.button_ReDispatch.Tag = "0";
            m_bWaitCallTaxi = false;
        }

        private void SetToNoPrepare()
        {
            this.button_Cancel.Enabled = false;
            this.button_StartCallTaxi.Enabled = false;
            m_bWaitCallTaxi = false;

        }

        public void setAlert()
        {
            m_dtRing = System.DateTime.Now.AddSeconds(15);

        }

        public void unsetAlert()
        {
            m_dtRing = System.DateTime.Now;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            Form_Main.Main.ChangeToMainScreen();
        }

        private void timer_Tick_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
                return;


            if (m_bWaitCallTaxi && m_dtWaitCallTaxiResult < System.DateTime.Now)
            {
                //Show System Err
                ShowNoTaxiMsg("叫車出現異常, 請稍後再試. 或來電中心由專人為您服務. 謝謝.", 3000, true);
                setAlert();
                m_bWaitCallTaxi = false;
            }


            if (panel_Msg.Visible && m_dtMsgShow < System.DateTime.Now)
            {
                ResetToPrepare();
                unsetAlert();


            }
            if (m_dtRing > System.DateTime.Now)
            {
                SoundManager.getInstance().playMsgRecv();
                
            }

        }

        private void button_Confirm_Click(object sender, EventArgs e)
        {
            unsetAlert();
            ResetToPrepare();
        }

        private void button_ReDispatch_Click(object sender, EventArgs e)
        {
            setReDispatchButton(2);
            unsetAlert();
            this.button_Confirm.Visible = false;
            m_bWaitCallTaxi = true;
            m_dtWaitCallTaxiResult = System.DateTime.Now.AddSeconds(60);
            SetToNoPrepare();
            Form_Main.Main.m_strLastCallTaxiCount = System.Convert.ToString(button_StartCallTaxi.Tag);
            string strResult = CmdTalker.getInstance().SendServerAndWaitResponse("CI1" + button_StartCallTaxi.Tag + "0" +m_strLastOrderID + StoreData.getInstance().TEL + "0", 5, "MOK", true);
            ShowNoTaxiMsg("正在為您擴大搜車中, 請稍候...", 65, false);

            if (strResult == "MOK")
            {
                //wait result

            }
            else
            {
                //fail , re send
            }
        }


    }
}
